package com.leps.nq.tx.core;

import com.leps.nq.common.util.DataUtils;
import com.leps.nq.tx.entity.PumpEffectivePoint;
import com.leps.nq.tx.vo.PumpDataVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultPumpDataHandler implements IPumpDataHandler {
    @Override
    public PumpDataVo mapDatagramToPumpDataVo(byte[] datagram, Integer pumpId, List<PumpEffectivePoint> effectivePoints, int firstAddress) {
        Map<String, Object> dataMap = new HashMap<>();
        List<Object> dataList = new ArrayList<>();
        for (PumpEffectivePoint effectivePoint : effectivePoints) {
            String dataType = effectivePoint.getDataType();
            Integer dataAddress = effectivePoint.getDataAddress();
            String spec = effectivePoint.getSpec();
            Object dataValue =  DataUtils.getDataValueAsObject(firstAddress, dataAddress, dataType, datagram);
            dataMap.put(spec, dataValue);
            dataList.add(dataValue);
        }
        PumpDataVo dataVo = new PumpDataVo();
        dataVo.setDataMap(dataMap);
        dataVo.setDataList(dataList);
        dataVo.setPumpId(pumpId);
        dataVo.setPumpEffectivePoints(effectivePoints);
        dataVo.setRunning((Integer) dataList.get(0));
        dataVo.setOpenSetupMinutes((Integer) dataList.get(1));
        dataVo.setCloseSetupMinutes((Integer) dataList.get(2));
        dataVo.setBroken((Integer) dataList.get(3));
        dataVo.setOpenRealMinutes((Integer) dataList.get(4));
        dataVo.setCloseRealMinutes((Integer) dataList.get(5));
        dataVo.setLiquidLevel((Integer) dataList.get(8));
        dataVo.setPressure((Integer) dataList.get(9));
        return dataVo;
    }
}
